<?php
namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class AuthGroupRule extends Base
{
//    use SoftDelete;
    // 设置当前模型的数据库连接
    /*
     * 根据角色id获取权限id
     */
    public function getIds($id) {
        return $this->where('role_id','eq',$id)->column('rule_id');
    }
    /*
 * 保存权限数据
 */
    public function saveData($role_id, $data) {
        if (empty($data)) {
            $this->where(['role_id' => $role_id])->delete();
            return true;
        }
        Db::startTrans();
        try {
            $this->where(['role_id' => $role_id])->delete();
            $insertData = [];
            foreach ($data as $val) {
                $insertData[] = ['role_id' => $role_id, 'rule_id' => $val];
            }
            $this->insertAll($insertData);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
        return true;
    }
}